package org.example.what_mall.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.what_mall.dto.*;
import org.example.what_mall.entity.*;
import org.example.what_mall.result.HttpResult;
import org.example.what_mall.service.IAdminService;
import org.example.what_mall.service.IMallService;
import org.example.what_mall.service.IUserService;
import org.example.what_mall.vo.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "管理接口")
@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
public class AdminController {
    private final IAdminService adminService;
    private final IUserService userService;
    private final IMallService mallService;

    @Operation(summary = "获取所有商品类型")
    @GetMapping("/getType")
    public HttpResult<List<Category>> getType() {
        return HttpResult.success(adminService.getTypes());
    }

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public HttpResult<UserVO> login(@RequestBody UserLoginForm form, HttpServletResponse response) {
        UserVO userVO = userService.login(form);
        String token = userVO.getToken();
        response.setHeader("token", token);
        return HttpResult.success(userVO);
    }

    @Operation(summary = "更改密码")
    @PostMapping("/changePwd")
    public HttpResult<Void> changePwd(@RequestBody UpdatePwdForm form) {
        adminService.changePwd(form);
        return HttpResult.ok();
    }

    @Operation(summary = "获取所有用户")
    @GetMapping("/allUser")
    public HttpResult<List<User>> getAllUser() {
        return HttpResult.success(adminService.getAllUser());
    }

    @Operation(summary = "搜索用户")
    @GetMapping("/searchUser")
    public HttpResult<List<User>> searchUser(@RequestParam("word") String word) {
        return HttpResult.success(adminService.searchUser(word));
    }

    @Operation(summary="删除用户")
    @DeleteMapping("/deleteUser")
    public HttpResult<Void> deleteUser(@RequestParam("id") Integer id) {
        adminService.deleteUser(id);
        return HttpResult.ok();
    }

    @Operation(summary = "添加商品类型")
    @PostMapping("/addType")
    public HttpResult<Void> addType(@RequestBody Category category) {
        adminService.addType(category);
        return HttpResult.ok();
    }

    @Operation(summary = "根据商品类型获取商品")
    @GetMapping("/getGoodsByType")
    public HttpResult<List<GoodsVO>> getGoodsByType(@RequestParam("typeId") Integer typeId) {
        return HttpResult.success(mallService.getGoodsByType(typeId));
    }

    @Operation(summary = "获取商品信息")
    @GetMapping("/getGoodsInfo")
    public HttpResult<GoodsDTO> getGoodsInfo(@RequestParam("id") Integer id ) {
        return HttpResult.success(mallService.getGoodsInfo(id));
    }

    @Operation(summary = "添加商品")
    @PostMapping("/addGoods")
    public HttpResult<Void> addGoods(@RequestBody GoodsForm form) {
        adminService.addGoods(form);
        return HttpResult.ok();
    }

    @Operation(summary = "更新商品")
    @PostMapping("/updateGoods")
    public HttpResult<Void> updateGoods(@RequestBody GoodsUpdateForm form) {
        adminService.updateGoods(form);
        return HttpResult.ok();
    }

    @Operation(summary = "添加商品规格")
    @PostMapping("/addSpec")
    public HttpResult<Spec> addSpec(@RequestBody Spec spec) {
        return HttpResult.success(adminService.addSpec(spec));
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("/deleteSpec")
    public HttpResult<Void> deleteSpec(@RequestBody Spec spec) {
        adminService.deleteSpec(spec);
        return HttpResult.ok();
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/deleteGoods")
    public HttpResult<Void> deleteGoods(@RequestParam("id") Integer id) {
        adminService.deleteGoods(id);
        return HttpResult.ok();
    }

    @Operation(summary = "获取所有订单")
    @GetMapping("/orders")
    public HttpResult<List<OrderInfoVO>> getOrderByState(@RequestParam("state") Integer state) {
        return HttpResult.success(adminService.getOrderByState(state));
    }

    @Operation(summary = "获取单个订单")
    @GetMapping("/order")
    public HttpResult<OneOrderInfoVO> getOrder(@RequestParam("id") Integer id) {
        return HttpResult.success(adminService.getOrder(id));
    }

    @Operation(summary = "修改订单")
    @PostMapping("/changeOrder")
    public HttpResult<Void> changeOrder(@RequestBody Order order) {
        adminService.changeOrder(order);
        return HttpResult.ok();
    }

    @Operation(summary = "删除订单")
    @DeleteMapping("/deleteOrder")
    public HttpResult<Void> deleteOrder(@RequestParam("id") Integer id) {
        adminService.deleteOrder(id);
        return HttpResult.ok();
    }

    @Operation(summary = "获取所有未评论消息")
    @GetMapping("/noReplyMsg")
    public HttpResult<List<MessageVO>> noReplyMsg() {
        return HttpResult.success(adminService.noReplyMessage());
    }

    @Operation(summary = "获取所有已回复消息")
    @GetMapping("/repliedMsg")
    public HttpResult<List<MessageVO>> repliedMsg() {
        return HttpResult.success(adminService.RepliedMessage());
    }

    @Operation(summary = "回复消息")
    @PostMapping("/reply")
    public HttpResult<Void> reply(@RequestBody ReplyForm form) {
        adminService.reply(form);
        return HttpResult.ok();
    }

}
